<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Baidu
 *
 * @author  add by dragon.hu@2015.11.30
 */
require_once 'BaseChannel.php';

//暴风
class Baidu implements BaseChannel 
{
	private $AppId = "7302510";
	private $Secretkey = "H2XpgePDa6BKBG8zhPjRC5z3Y4rgiFI1";  //secretkey
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="H2XpgePDa6BKBG8zhPjRC5z3Y4rgiFI1"; // secretkey
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey="H2XpgePDa6BKBG8zhPjRC5z3Y4rgiFI1"; //secretkey
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("baidu", $_REQUEST);
		
		$Result = array();//存放结果数组
		$OrderSerial='';
		$CooperatorOrderSerial='';
		$Sign='';
		$Content='';
		
		//获取参数  提供两种获取参数方式
		//1.Request方式获取请求参数
		//if(isset($_REQUEST['OrderSerial']))
		//$OrderSerial= $_REQUEST['OrderSerial'];
		//if(isset($_REQUEST['CooperatorOrderSerial']))
		//$CooperatorOrderSerial= $_REQUEST['CooperatorOrderSerial'];
		//if(isset($_REQUEST['Sign']))
		//$Sign= $_REQUEST['Sign'];
		//if(isset($_REQUEST['Content']))
		//$Content= $_REQUEST['Content'];//Content通过Request读取的数据已经自动解码
		
		//2.读取POST流方式获取请求参数
		$inputParams = file_get_contents('php://input');
		$connectorParam = "&";
		$spiltParam="=";
		if(!empty($inputParams)){
			if(strpos($inputParams,$connectorParam) && strpos($inputParams,$spiltParam)){
				$list=explode($connectorParam,$inputParams);
				//print(count($list));
				for($i=0;$i<count($list);$i++){
					$kv=explode($spiltParam,$list[$i]);
					if(count($kv)>1){
						if($kv[0]=="OrderSerial"){
							$OrderSerial=$kv[1];
						}else if($kv[0]=="CooperatorOrderSerial"){
							$CooperatorOrderSerial=$kv[1];
						}else if($kv[0]=="Sign"){
							$Sign=$kv[1];
						}else if($kv[0]=="Content"){
							$Content=urldecode($kv[1]);	//读取POST流的方式需要进行UrlDecode解码操作
							//print($Content);
						}
					}
				}
			}
		}
		//参数检测
		if(empty($OrderSerial)||empty($CooperatorOrderSerial)||empty($Sign)
				||empty($Content)){
			$Result["AppID"] =  $this->AppId;
			$Result["ResultCode"] =  1000;
			$Result["ResultMsg"] =  urlencode("接收参数失败");
			$Result["Sign"] =  md5($this->AppId.$Result["ResultCode"].$this->Secretkey);
			$Result["Content"] =  "";
			$Res = json_encode($Result);
			return urldecode($Res);
		}
		
		//检测请求数据签名是否合法
		if($Sign != md5($this->AppId.$OrderSerial.$CooperatorOrderSerial.$Content.$this->Secretkey)){
			$Result["AppID"] =  $this->AppId;
			$Result["ResultCode"] =  1001;
			$Result["ResultMsg"] =  urlencode("签名错误");
			$Result["Sign"] =  md5($this->AppId.$Result["ResultCode"].$this->Secretkey);
			$Result["Content"] =  "";
			$Res = json_encode($Result);
			return urldecode($Res);
		}
		
		//base64解码
		$Content=base64_decode($Content);
		//json解析
		$Item=extract(json_decode($Content,true));
		//$UID $MerchandiseName $OrderMoney $StartDateTime $BankDateTime $OrderStatus $StatusMsg $ExtInfo $VoucherMoney
		//print($UID);
		//根据获取到的数据，执行业务处理
		
		/****************************************业务逻辑部分***********************************************/
		if ($OrderStatus == 1)
		{
			$status = STATUS_SUCCESS;
		}
		else
		{
			$status = STATUS_FAILD;
		}
		if(true == $orderLog->recordOrder($status, $OrderMoney, $CooperatorOrderSerial, "baidu", $platform,$OrderSerial))
		{
			$orderLog->log('baidu','platform:'.$platform.'amount:'.($OrderMoney).' order_id:'.$partnerOrder.' success');
		}else
		{
			$orderLog->log('baidu','platform:'.$platform.'amount:'.($OrderMoney).' order_id:'.$partnerOrder.' fail');
			$Result["AppID"] =  $this->AppId;
			$Result["ResultCode"] =  1002;       //其他错误的返回码！！！！！！！
			$Result["ResultMsg"] =  urlencode("其他错误");
			$Result["Sign"] =  md5($this->AppId.$Result["ResultCode"].$this->Secretkey);
			$Result["Content"] =  "";
			$Res = json_encode($Result);
			return urldecode($Res);
		}
		/************************************************************************************************/
		
		//返回成功结果
		$Result["AppID"] =  $this->AppId;
		$Result["ResultCode"] =  1;
		$Result["ResultMsg"] =  urlencode("成功");
		$Result["Sign"] =  md5($this->AppId.$Result["ResultCode"].$this->Secretkey);
		$Result["Content"] = "";
		$Res = json_encode($Result);
		return urldecode($Res);
	}
	
}